package com.ruoyi.zsyh.controller.applet.agent;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 市县管理商Controller
 * 
 */
@Controller
@RequestMapping("/applet/zsyh/raker")
public class ZsyhRakerAppletController extends BaseController
{

    @Autowired
    private IZsyhPlatformUserService zsyhPlatformUserService;

    @Autowired
    private IZsyhUserRepairstoreService zsyhUserRepairstoreService;



   /* *//**
     * 邀请市县管理商  现在已经没有这个功能了
     * @return
     *//*
    @RequestMapping("/inviteAgentCity")
    @ResponseBody
    @Transactional
    public AjaxResult inviteAgentCity(String agentId, String name, String phone, String areaJson)throws Exception
    {
        ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByPhone(phone);
        if(store != null){
            return error("手机号已存在！");
        }
        ZsyhPlatformUser user = zsyhPlatformUserService.selectZsyhPlatformUserByPhone(phone);
        Date date = new Date();

        raker = new ZsyhUserRaker();
        raker.setId(IdWorker.getId());
        raker.setRakerPerson(name);
        raker.setRakerPhone(phone);
        raker.setRakerState("1"); //未入住
        raker.setAgentId(agentId);
        raker.setCreateTime(date);


        if(user == null){
            user = new ZsyhPlatformUser();
            user.setId(IdWorker.getId());
            user.setUserPhone(phone);
            user.setUserType("1");
            user.setIsCity("1");
            user.setCreateTime(date);
            zsyhPlatformUserService.insertZsyhPlatformUser(user);
        }else{
            ZsyhUserRepairstore store = zsyhUserRepairstoreService.selectZsyhUserRepairstoreByUserId(user.getId());
            if(store != null){
                String storeAgentId = store.getAgentId();
                if(!agentId.equals(storeAgentId)){
                    return error("邀请失败，当前手机号已经入驻为其他会长下的店铺。");
                }
                ZsyhUserRepairstore store1 = new ZsyhUserRepairstore();
                store1.setId(store.getId());
                store1.setIsRaker("1");
                zsyhUserRepairstoreService.updateZsyhUserRepairstoreApplet(store1);
            }
            ZsyhPlatformUser user1 = new ZsyhPlatformUser();
            user1.setId(user.getId());
            user1.setIsCity("1");
            zsyhPlatformUserService.updateZsyhPlatformUser(user1);
            raker.setRakerState("2"); //已入住
        }

        raker.setUserId(user.getId());





        //解析产品json数据
        List<Map<String, Object>> list = JSON.parseObject(areaJson, new TypeReference<List<Map<String, Object>>>() {});
        List<ZsyhUserRakearea> areaList = new ArrayList<>();

        for(Map<String, Object> a:list){
            ZsyhUserRakearea area = new ZsyhUserRakearea();
            String pCode = String.valueOf(a.get("pCode"));
            String pName = String.valueOf(a.get("pName"));
            String cCode = String.valueOf(a.get("cCode"));
            String cName = String.valueOf(a.get("cName"));
            String aCode = String.valueOf(a.get("aCode"));
            String aName = String.valueOf(a.get("aName"));
            area.setId(IdWorker.getId());
            area.setCreateTime(raker.getCreateTime());
            area.setRakerId(raker.getId());
            area.setRakerProvince(pCode);
            area.setRakerProvinceName(pName);
            area.setRakerCity(cCode);
            area.setRakerCityName(cName);
            area.setRakerDistrict(aCode);
            area.setRakerDistrictName(aName);
            areaList.add(area);
        }
        raker.setZsyhUserRakeareaList(areaList);
        zsyhUserRakerService.insertZsyhUserRaker(raker);

        return success("成功");
    }*/

}
